package com.fjrz.bbxwj.main.video

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.renderscript.Allocation
import android.renderscript.Element
import android.renderscript.RenderScript
import android.renderscript.ScriptIntrinsicBlur
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.drawToBitmap
import com.blankj.utilcode.util.ToastUtils
import com.module.lemlin.base.loadCircleImage
import com.module.lemlin.base.loadCropImage
import com.shuyu.gsyvideoplayer.utils.GSYVideoType
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView
import com.fjrz.bbxwj.R
import com.fjrz.bbxwj.entity.VideoList
import com.module.lemlin.base.loadCenterImage
import com.module.lemlin.custom.TagTextView

class CustomGSYVideoPlayer : StandardGSYVideoPlayer {

    private lateinit var ibControlsStart: ImageButton
    private lateinit var ivControlsThumb: ImageView

    //用户头像
    private lateinit var ivControlsHeads: ImageView

    //关注按钮
    private lateinit var ivControlsAttention: ImageView

    //点赞
    private lateinit var tvControlsApprove: TextView

    //评论
    private lateinit var tvControlsDiscuss: TextView

    //分享
    private lateinit var tvControlsShare: TextView

    //用户名称
    private lateinit var tvControlsName: TextView

    //标题内容
    private lateinit var tvControlsTitle: TagTextView

    private lateinit var llControlsAd: LinearLayout

    //广告标题
    private lateinit var tvControlsAdTitle: TextView

    //广告详情
    private lateinit var tvControlsAdDetails: TextView

    private var mVideoList: VideoList? = null

    constructor(context: Context?, fullFlag: Boolean?) : super(
        context, fullFlag
    )

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(
        context, attrs
    )

    override fun getLayoutId(): Int {
        return R.layout.custom_controls_video
    }

    override fun init(context: Context) {
        super.init(context)
        ibControlsStart = findViewById(R.id.ibControlsStart)
        ivControlsThumb = findViewById(R.id.ivControlsThumb)
        ivControlsHeads = findViewById(R.id.ivControlsHeads)
        ivControlsAttention = findViewById(R.id.ivControlsAttention)

        tvControlsApprove = findViewById(R.id.tvControlsApprove)
        tvControlsDiscuss = findViewById(R.id.tvControlsDiscuss)
        tvControlsShare = findViewById(R.id.tvControlsShare)
        tvControlsName = findViewById(R.id.tvControlsName)
        tvControlsTitle = findViewById(R.id.tvControlsTitle)

        llControlsAd = findViewById(R.id.llControlsAd)
        tvControlsAdTitle = findViewById(R.id.tvControlsAdTitle)
        tvControlsAdDetails = findViewById(R.id.tvControlsAdDetails)

        ibControlsStart.setOnClickListener(this)
//        ivControlsHeads.setOnClickListener(this)

        if (mThumbImageViewLayout != null &&
            (mCurrentState == -1 || mCurrentState == GSYVideoView.CURRENT_STATE_NORMAL || mCurrentState == GSYVideoView.CURRENT_STATE_ERROR)
        ) {
            mThumbImageViewLayout.visibility = View.VISIBLE
        }

    }

    override fun touchSurfaceMoveFullLogic(
        absDeltaX: Float,
        absDeltaY: Float
    ) {
        super.touchSurfaceMoveFullLogic(absDeltaX, absDeltaY)
        //不给触摸快进，如果需要，屏蔽下方代码即可
        mChangePosition = false
        //不给触摸音量，如果需要，屏蔽下方代码即可
        mChangeVolume = false
        //不给触摸亮度，如果需要，屏蔽下方代码即可
        mBrightness = false
    }

    override fun onClickUiToggle(e: MotionEvent) {
        touchDoubleUp(e)
        if (mCurrentState == 0) {
            startPlayLogic()
        }
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        if (v?.id == R.id.ibControlsStart) {
            onVideoResume(false)
        } else if (v?.id == R.id.ivControlsHeads) {
            ToastUtils.showShort(mVideoList?.title)
        }

    }

    override fun resolveUIState(state: Int) {
        super.resolveUIState(state)
        if (state == CURRENT_STATE_PLAYING) {
            ibControlsStart.visibility = View.GONE
        } else if (state == CURRENT_STATE_PAUSE) {
            ibControlsStart.visibility = View.VISIBLE
        }

        if (state == CURRENT_STATE_PLAYING) {

//            ivControlsThumb.drawable?.let {
//                val m: Matrix = ivControlsThumb.imageMatrix
//                val values = FloatArray(10)
//                m.getValues(values)
//                LogUtils.e("====${ivControlsThumb.drawable?.bounds?.width()?:0 / values[0]} == ${ivControlsThumb.drawable?.bounds?.height()?:0 / values[4]}")
//            }

            GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_DEFAULT)
//            if (currentVideoHeight.toFloat() / currentVideoWidth.toFloat() >= 16f / 9f) {
//                GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_FULL)
//            } else {
//                GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_DEFAULT)
//            }
        }
    }

    fun setVideoList(videoList: VideoList) {
        this.mVideoList = videoList
        tvControlsApprove.text = quantityUnit(videoList.likes)
        tvControlsDiscuss.text = quantityUnit(videoList.comments)
        tvControlsShare.text = quantityUnit(videoList.shares)
        tvControlsName.text = videoList.user.nickname
        if (videoList.type == 2) {
            llControlsAd.visibility = View.VISIBLE
            tvControlsAdTitle.text = videoList.adv_title
            tvControlsTitle.setTextTag(videoList.description, mutableListOf("广告"))
        } else {
            llControlsAd.visibility = View.GONE
            tvControlsTitle.text = videoList.description
        }
        ivControlsAttention.visibility = if (videoList.followed == 1)
            View.GONE else View.VISIBLE
        ivControlsThumb.loadCenterImage(videoList.main_image)
        //ivControlsThumb.loadCropImage(videoList.main_image)
        ivControlsHeads.loadCircleImage(videoList.user.avatar)

        val drawable: Drawable = if (videoList.like == 1)
            resources.getDrawable(R.mipmap.ic_17208)
        else resources.getDrawable(R.mipmap.ic_17207)
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        tvControlsApprove.setCompoundDrawables(null, drawable, null, null)


//       val bitmap:Bitmap = BitmapFactory.decodeResource(resources,R.mipmap.ic_17523)
//
//        val rs:RenderScript = RenderScript.create(context)
//        val input:Allocation = Allocation.createFromBitmap(rs,bitmap,Allocation.MipmapControl.MIPMAP_NONE,Allocation.USAGE_SCRIPT)
//        val output:Allocation = Allocation.createTyped(rs,input.type)
//        val script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs))
//        script.setRadius(10f)
//        script.setInput(input)
//        script.forEach(output)
//        output.copyTo(bitmap)
//
//        llControlsAd.background = BitmapDrawable(resources,bitmap)
//        rs.destroy()
    }

    private fun quantityUnit(quantity: Int): String {
        if (quantity >= 10000) {
            return String.format("%.1fw", quantity.toDouble() / 10000)
        }
        return quantity.toString()
    }

}